package com.group9.controller;

import com.group9.dto.FruitCartInfo;
import com.group9.pojo.FruitCart;
import com.group9.service.FruitCartInfoService;
import com.group9.service.FruitCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Lcouncillor on 2020/5/4.
 */
@Controller
@RequestMapping("shoppingcart")
public class ShoppingCartController {

    @Autowired
    FruitCartInfoService fruitCartInfoService;
    @Autowired
    FruitCartService fruitCartService;

    @RequestMapping(value = "showinfos")
    @ResponseBody
    public List<FruitCartInfo> showCartInfos(){
        //System.out.println(fruitCartInfoService.selectByCustomerId(1001));
        return fruitCartInfoService.selectByCustomerId(1001);
    }

    @RequestMapping(value = "savecart")
    @ResponseBody
    public String saveCart(@RequestBody List<FruitCartInfo> fruitCartInfos){

        for ( int i=0; i<fruitCartInfos.size(); i++){
            FruitCart fruitCart = new FruitCart();
            fruitCart.setCartId(fruitCartInfos.get(i).getCartId());
            fruitCart.setFruitAmount(fruitCartInfos.get(i).getFruitAmount());

            int row = fruitCartService.updateCart(fruitCart);
        }

        return "success";
    }

    @RequestMapping(value = "pay")
    @ResponseBody
    public String pay(@RequestBody List<FruitCartInfo> fruitCartInfos){
        System.out.println(fruitCartInfos);
        return "success";
    }


    @RequestMapping(value = "deletecart")
    @ResponseBody
    public String deletecart(Integer cartId){
        int row = fruitCartService.deleteByCartId(cartId);
        if (row == 1){
            return "success";
        }else {
            return "fail";
        }
    }

    @RequestMapping(value = "insertcart")
    @ResponseBody
    public String addCart(Integer fruitId, Integer fruitAmount){

//        System.out.println("fruitId:"+fruitId+",fruitAmount:"+fruitAmount);
//        return "success";
        Integer customerId = 1001;
        int resultRow = fruitCartService.insertCartByFruitIdAndAmount(fruitId,customerId,fruitAmount);

        if (resultRow==1){
            return "success";
        }else{
            return "fail";
        }
    }

}
